package com.yunji.order.context.convert;

import com.yunji.base.cache.BaseCache;
import com.yunji.base.domain.BelongDO;
import com.yunji.base.enums.ScaleTypes;
import com.yunji.common.enums.business.TradeMode;
import com.yunji.common.enums.business.TradeTypes;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.coupon.dto.OrderScaleDTO;
import com.yunji.order.domain.PayoutDO;
import com.yunji.order.domain.TradeDO;
import com.yunji.order.emuns.PayMode;
import com.yunji.order.emuns.PayTypes;
import com.yunji.order.vo.OrderScaleVO;
import com.yunji.order.vo.TradeVO;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : peter-zhu
 * @date : 2025/3/24 14:30
 * @description : TODO
 **/
public class TradeConvert {

    public static TradeDO convertByPayout(PayoutDO payout) {
        TradeDO trade = new TradeDO();
        trade.setTransactionNo(payout.getPayoutCode());
        trade.setOpenId(payout.getPhone());
        trade.setApiTime(payout.getPayTime());
        trade.setApiCode(payout.getPayCode());
        trade.setTradeAmount(payout.getAmount());
        BelongDO belong = BaseCache.getBelong(payout.getBelongId());
        trade.setBelongId(belong.getBelongId());
        trade.setAccountId(belong.getBelongId());
        trade.setAccountType(belong.getBelongType());
        trade.setAccountName(belong.getBelongName());
        trade.setAccountTel(payout.getPhone());
        trade.setBalance(belong.getRemainAmount().add(payout.getAmount()));
        return trade;
    }

    public static TradeDO convertTrade(OrderScaleDTO sc, TradeDO defaultTrade) {
        TradeDO newTrade = defaultTrade.clone();
        newTrade.setTradeType(sc.getTradeType());
        newTrade.setTradeAmount(sc.getAmt());
        newTrade.setAccountId(sc.getAccountId());
        newTrade.setAccountType(sc.getAccountType());
        newTrade.setAccountName(sc.getAccountName());
        newTrade.setAccountTel(sc.getAccountTel());
        newTrade.setBalance(sc.getBalance());
        newTrade.setBelongId(sc.getAccountId());
        return newTrade;
    }

    public static void convertOrderScaleVO(OrderScaleVO vo) {
        vo.setAccountTypeDesc(UserType.getText(vo.getAccountType()));
        vo.setTradeType(Objects.requireNonNull(TradeTypes.get(vo.getTradeType())).getCode());
        vo.setAccountScaleTypeDesc(ScaleTypes.get(vo.getAccountScaleType()));
    }

    public static void convertOrderScaleVOs(List<OrderScaleVO> vos) {
        vos.forEach(TradeConvert::convertOrderScaleVO);
    }

    public static TradeVO domain2VO(TradeDO domain) {
        TradeVO vo = new TradeVO();
        BeanUtils.clone(domain, vo);
        vo.setTradeModeDesc(Objects.requireNonNull(TradeMode.get(domain.getTradeMode())).getText());
        vo.setPayTypeDesc(PayTypes.getText(domain.getPayType()));
        vo.setPayModeDesc(PayMode.getText(domain.getPayMode()));
        return vo;
    }

    public static List<TradeVO> domain2VOS(List<TradeDO> domains) {
        return domains.stream().map(TradeConvert::domain2VO).collect(Collectors.toList());
    }
}
